package com.easemob.helpdesk.widget;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easemob.helpdesk.utils.DateUtils;
import com.easemob.helpdesk.utils.DisplayUtil;
import com.easemob.helpdesk.utils.TimeInfo;
import com.easemob.helpdesk.widget.pickerview.SimplePickerView;
import com.housekeeperapp.R;

import java.util.ArrayList;

/**
 * Created by wsj on 2018/6/4.
 */

public class TimeSelectView {

    private Context mContext;
    private Object mParent;

    private ViewGroup decorView;
    private ViewGroup rootView;

    public TimeSelectView(Context context, Object parent){
        this.mContext = context;
        this.mParent = parent;

    }


    public void showSearchView(){
        if (decorView != null && isShowing()) {
            hideSearchView();
            return;
        }

        if(decorView!=null && rootView!=null){
            rootView.setVisibility(View.VISIBLE);
            return;
        }

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        decorView = (ViewGroup) ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content);
        rootView = (ViewGroup) layoutInflater.inflate(R.layout.popup_time_filter, decorView, false);

        FrameLayout frameLayout = (FrameLayout) rootView.findViewById(R.id.popup_vill_bg);

        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSearchView();
            }
        });


        Button rl_clear_btn = (Button) rootView.findViewById(R.id.clear_data_btn);
        Button rl_ok_data_btn = (Button) rootView.findViewById(R.id.ok_data_btn);

        final RadioButton rl_radio0 = (RadioButton) rootView.findViewById(R.id.radio0);
        final RadioButton rl_radio1 = (RadioButton) rootView.findViewById(R.id.radio1);
        final RadioButton rl_radio2 = (RadioButton) rootView.findViewById(R.id.radio2);
        final RadioButton rl_radio3 = (RadioButton) rootView.findViewById(R.id.radio3);
        final RadioButton rl_radio4 = (RadioButton) rootView.findViewById(R.id.radio4);

        //int outerHeight = DisplayUtil.px2dip(mContext, mContext.getResources().getDisplayMetrics().heightPixels)-54;
        final RadioGroup radiogroup = (RadioGroup) rootView.findViewById(R.id.radiogroup);
        final RadioGroup radiogroup1 = (RadioGroup) rootView.findViewById(R.id.radiogroup1);

        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if(checkedId==R.id.radio0){
                    if (rl_radio0.isChecked())
                        radiogroup1.clearCheck();
                }else if(checkedId==R.id.radio1){
                    if (rl_radio1.isChecked())
                        radiogroup1.clearCheck();
                }else if(checkedId==R.id.radio2){
                    if (rl_radio2.isChecked())
                        radiogroup1.clearCheck();
                }
      /*          switch (checkedId) {
                    case R.id.radio0:
                        if (rl_radio0.isChecked())
                            radiogroup1.clearCheck();
                        break;
                    case R.id.radio1:
                        if (rl_radio1.isChecked())
                            radiogroup1.clearCheck();
                        break;
                    case R.id.radio2:
                        if (rl_radio2.isChecked())
                            radiogroup1.clearCheck();
                        break;
                    default:break;
                }*/
            }
        });


        radiogroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if(checkedId==R.id.radio3){
                    if (rl_radio3.isChecked())
                        radiogroup.clearCheck();
                }else if(checkedId==R.id.radio4){
                    if (rl_radio4.isChecked())
                        radiogroup.clearCheck();
                }

            }
        });

        //clear
        rl_clear_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {reset();
            }
        });

        rl_ok_data_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimeInfo info = null;

                if(radiogroup.getCheckedRadioButtonId()==R.id.radio0){
                    info = DateUtils.getTodayStartAndEndTime();
                }else if(radiogroup.getCheckedRadioButtonId()==R.id.radio1){
                    info = DateUtils.getYesterdayStartAndEndTime();
                }else if(radiogroup.getCheckedRadioButtonId()==R.id.radio2){
                    info = DateUtils.getTimeInfoByCurrentWeek(0);
                }
//                switch (radiogroup.getCheckedRadioButtonId()) {
//                    case R.id.radio0://今天
//                        info = DateUtils.getTodayStartAndEndTime();
//                        break;
//                    case R.id.radio1://昨天
//                        info = DateUtils.getYesterdayStartAndEndTime();
//                        break;
//                    case R.id.radio2://本周
//                        info = DateUtils.getTimeInfoByCurrentWeek(0);
//                        break;
//                    default:break;
//                }


                if(radiogroup1.getCheckedRadioButtonId()==R.id.radio3){
                    info = DateUtils.getTimeInfoByCurrentMonth();
                }else if(radiogroup1.getCheckedRadioButtonId()==R.id.radio4){
                    info = DateUtils.getTimeInfoByLastMonth();
                }


                hideSearchView();

                if (mParent instanceof TimeSelectView.searchListener){
                    ((TimeSelectView.searchListener)mParent).addrSearchRes(info);
                }


                //currentSessionFragment.getAdapter().getFilter().filter(filterStr);
            }
        });


        decorView.addView(rootView);

        showView();

    }

    public void reset(){

        final RadioButton rl_radio2 = (RadioButton) rootView.findViewById(R.id.radio2);
        final RadioGroup radiogroup = (RadioGroup) rootView.findViewById(R.id.radiogroup);

        radiogroup.check(rl_radio2.getId());

        TimeInfo info = DateUtils.getTimeInfoByCurrentWeek(0);

        if (mParent instanceof TimeSelectView.searchListener){
            ((TimeSelectView.searchListener)mParent).addrSearchRes(info);
        }
    }

    public boolean isShowing() {
        return rootView.getVisibility() == View.VISIBLE;
    }

    public void hideSearchView(){
        rootView.setVisibility(View.GONE);
    }

    public void showView() {
        rootView.setVisibility(View.VISIBLE);
    }

    public interface searchListener{
        void addrSearchRes(TimeInfo info);
    }
}
